package com.smart.reflect;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class PrivateCarReflect {

    public static void main(String[] args) {
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        try {
            Class<?> clazz = classLoader.loadClass("com.smart.reflect.PrivateCar");
            PrivateCar car = (PrivateCar) clazz.newInstance();
            Field colorField = clazz.getDeclaredField("color");
            colorField.setAccessible(true);
            colorField.set(car, "红色");

            Method driveMethod = clazz.getDeclaredMethod("drive", (Class<?>[]) null);
            driveMethod.setAccessible(true);
            driveMethod.invoke(car, (Object[]) null);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

}
